package com.gx.mxbbx.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gx.mxbbx.dao.SysUserMapper;
import com.gx.mxbbx.po.SysUser;
import com.gx.mxbbx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.sql.Timestamp;
import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 根据用户ID查找用户信息
     *
     * @param userId
     * @return
     */

    @Override
    public SysUser findUserByUserId(Integer userId, String userPassword) {
        SysUser sysUser = sysUserMapper.findUserByUserId(userId, userPassword);
        return sysUser;
    }

    /**
     * 查找用户列表
     *
     * @return
     */
    public PageInfo<SysUser> getUserList(String userName, Integer currentPage, Integer limit) {
        //利用PageHelper分页查询 注意：这个一定要放查询语句的前一行,否则无法进行分页,因为它对紧随其后第一个sql语句有效
        PageHelper.startPage(currentPage, limit);
        List<SysUser> userList = sysUserMapper.getUserList(userName);
        PageInfo<SysUser> pageInfo = new PageInfo<SysUser>(userList);
        return pageInfo;
    }

    //添加用户
    public void addUser(SysUser sysUser) {
        sysUser.setCreateTime(new Timestamp(System.currentTimeMillis()));
        sysUser.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        sysUserMapper.addUser(sysUser);
    }

    //删除用户
    public void delete(Integer id) {
        sysUserMapper.delete(id);
    }

    //查询用户修改信息

    public SysUser getUserById(Integer id) {
        SysUser sysUser = sysUserMapper.getUserById(id);
        return sysUser;
    }

    public void edit(SysUser sysUser) {
        sysUser.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        sysUserMapper.edit(sysUser);
    }

}
